package com.wsq.retrofitdemo.retrofit;

import android.os.Handler;
import android.util.Log;

import com.wsq.retrofitdemo.model.request.IFlyAudioAddReq;
import com.wsq.retrofitdemo.model.request.IFlyTrainTextReq;
import com.wsq.retrofitdemo.model.request.IFlyUserVoiceReq;
import com.wsq.retrofitdemo.model.response.IFlyAudioAddRes;
import com.wsq.retrofitdemo.model.response.IFlyBindResponse;
import com.wsq.retrofitdemo.model.request.IFlyUserBind;
import com.wsq.retrofitdemo.model.response.IFlyTrainTextRes;
import com.wsq.retrofitdemo.model.response.IFlyUserVoiceRes;
import com.wsq.retrofitdemo.utils.Constants;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Date:2021/9/1
 * Time:17:17
 * author:wushengqi
 */
public class IFlyHttpClient {
    private static volatile IFlyHttpClient mIFlyHttpClient;
    Retrofit2Service retrofit2Service;

    private IFlyHttpClient(){
        LoggerInterceptor interceptor = new LoggerInterceptor();
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.addInterceptor(interceptor);
        client.addInterceptor(httpLoggingInterceptor);

        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Retrofit2Service.IFLY_BASE_PATH)
                .client(client.build())
                .build();

        retrofit2Service = retrofit.create(Retrofit2Service.class);
    }

    public static IFlyHttpClient getInstance(){
        if (mIFlyHttpClient == null){
            synchronized (IFlyHttpClient.class){
                if (mIFlyHttpClient == null){
                    mIFlyHttpClient = new IFlyHttpClient();
                }
            }
        }

        return mIFlyHttpClient;
    }


    public void iflyBindUser(IFlyUserBind iFlyUserBind){

        iFlyUserBind = new IFlyUserBind();
        iFlyUserBind.appid = "123456";
        iFlyUserBind.guid = "123456";
        iFlyUserBind.phone_on = "13340000000";
        iFlyUserBind.third_user_id = "123456";
        iFlyUserBind.sign();

        Handler handler = new Handler();



        Call<IFlyBindResponse> call = retrofit2Service.bindUser(iFlyUserBind.getMap());
        call.enqueue(new Callback<IFlyBindResponse>() {
            @Override
            public void onResponse(Call<IFlyBindResponse> call, Response<IFlyBindResponse> response) {
                if (response.body() != null){
                    IFlyBindResponse iFlyBindResponse = response.body();
                    Log.e("Thread", Thread.currentThread().getName());
                }

            }

            @Override
            public void onFailure(Call<IFlyBindResponse> call, Throwable throwable) {
                Log.e("err", throwable.getMessage());
            }
        });
    }

    public void getIFlyUserVoices(IFlyUserVoiceReq req){
        req = new IFlyUserVoiceReq();
        req.third_user_id = "12321412412";
        req.count = 10;
        req.offset = 0;
        req.sign();


        Call<IFlyUserVoiceRes> call = retrofit2Service.getUserVoices(req.getMap());
        call.enqueue(new Callback<IFlyUserVoiceRes>() {
            @Override
            public void onResponse(Call<IFlyUserVoiceRes> call, Response<IFlyUserVoiceRes> response) {
                if (response.code() == 200){
//                    IFlyUserVoiceRes res = response.body();
//                    if (res != null){
//
//                    }

                }
            }

            @Override
            public void onFailure(Call<IFlyUserVoiceRes> call, Throwable throwable) {

            }
        });


    }

    public void getTrainTexts(IFlyTrainTextReq req){
        req = new IFlyTrainTextReq();
        req.category_id = Constants.CATEGORY_ID;
        req.sign();

        Call<IFlyTrainTextRes> call = retrofit2Service.getTrainTexts(req.getMap());
        call.enqueue(new Callback<IFlyTrainTextRes>() {
            @Override
            public void onResponse(Call<IFlyTrainTextRes> call, Response<IFlyTrainTextRes> response) {

            }

            @Override
            public void onFailure(Call<IFlyTrainTextRes> call, Throwable throwable) {

            }
        });

    }

    public void uploadVoice(){
        File file = new File("");
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        // MultipartBody.Part  和后端约定好Key，这里的partName是用file
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        // 添加描述
//        String descriptionString = "hello, 这是文件描述";
//        RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);

    }

    /**
     * 添加训练音频
     */
    public void audioAdd(IFlyAudioAddReq req){

        Call<IFlyAudioAddRes> call = retrofit2Service.audioAdd(req.getMap());
        call.enqueue(new Callback<IFlyAudioAddRes>() {
            @Override
            public void onResponse(Call<IFlyAudioAddRes> call, Response<IFlyAudioAddRes> response) {

            }

            @Override
            public void onFailure(Call<IFlyAudioAddRes> call, Throwable throwable) {

            }
        });
    }

    /**
     * 获取音频检查结果
     */
    public void checkAudioDitail(){

    }
}
